package com.mql.learnSpringCloud.controller;

import com.example.cloudapicommons.entity.CommonResult;
import com.example.cloudapicommons.entity.PaymentEntity;
import com.mql.learnSpringCloud.service.NacosProviderService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@EnableDiscoveryClient
@RequestMapping("/maQingLin")
public class NacosProviderController {
    @Resource
    NacosProviderService nacosProviderService;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/get/{id}")
    public CommonResult<PaymentEntity> getPayment(@PathVariable("id") Integer id) {
        CommonResult<PaymentEntity> commonResult=new CommonResult<PaymentEntity>();
        PaymentEntity paymentEntity=new PaymentEntity();
        paymentEntity=nacosProviderService.getPaymentEntity(id);
        if (paymentEntity!=null){
            commonResult.setCode(200);
            commonResult.setMessage("查询成功"+serverPort);
            commonResult.setData(paymentEntity);
        }
        else {
            commonResult.setCode(500);
            commonResult.setMessage("查询失败");
            commonResult.setData(null);
        }
        return commonResult;
    }
    @PostMapping("/save")
    public CommonResult<PaymentEntity> savePayment(PaymentEntity paymentEntity) {
        CommonResult<PaymentEntity> commonResult=new CommonResult<PaymentEntity>();
        int result=0;
        result=nacosProviderService.add(paymentEntity);
        if(result>0){
            commonResult.setCode(200);
            commonResult.setMessage("添加成功");
            commonResult.setData(null);
        }
        else {
            commonResult.setCode(500);
            commonResult.setMessage("添加失败");
            commonResult.setData(null);
        }
        return commonResult;
    }
}
